<template>
  <el-dialog @close="close" v-model="dialogVisible" :title="title" width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px">
      <el-form-item label="设备平台" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入设备平台名称" />
      </el-form-item>
      <el-form-item label="操作类" prop="classconfig">
        <el-input v-model="ruleForm.classconfig" placeholder="请输入操作类" />
      </el-form-item>
      <el-form-item label="版本" prop="verson">
        <el-input v-model="ruleForm.verson" placeholder="请输入版本" />
      </el-form-item>
      <el-form-item label="设备状态" prop="status">
        <el-switch
          v-model="ruleForm.status"
          inline-prompt
          active-text="启用"
          inactive-text="禁用"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="saveLoading" @click="handleConfim(ruleFormRef)">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ElMessageBox, ElMessage, FormInstance, ElNotification } from 'element-plus'
import { reactive, ref } from 'vue'
import { deptData } from '@/mock/system'
import { addEquipment, editEquipment } from '@/api/admin'

const ruleFormRef = ref<FormInstance>()
const dialogVisible = ref<boolean>(false)
const deptDataOptions = ref(deptData)
const title = ref('新增设备平台')
const rules = reactive({
  name: [{ required: true, message: '请输入设备平台名称', trigger: 'blur' }],
  classconfig: [{ required: true, message: '请输入操作类', trigger: 'blur' }],
  verson: [{ required: true, message: '请输入版本', trigger: 'blur' }],
})
const ruleForm = reactive({
  id: '',
  name: '',
  classconfig: '',
  verson: '',
  status: 1,
})
const saveLoading = ref(false)
const emit = defineEmits(['refresh'])

function close() {
  ruleFormRef.value.resetFields()
  Object.keys(ruleForm).forEach((key) => {
    if (key == 'status') ruleForm[key] = 1
    else ruleForm[key] = null
  })
}

const show = (item = {}) => {
  title.value = '新增设备平台'
  if (item.id) {
    title.value = '编辑设备平台'
    Object.keys(ruleForm).forEach((key) => {
      ruleForm[key] = item[key]
    })
  }
  dialogVisible.value = true
}

const handleConfim = async (done: () => void) => {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      saveLoading.value = true
      if (ruleForm.id) {
        //编辑
        editEquipment(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: '编辑成功',
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
          }
          dialogVisible.value = false
          saveLoading.value = false
        })
      } else {
        //新增
        addEquipment(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: '添加成功',
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
          }
          dialogVisible.value = false
          saveLoading.value = false
        })
      }
    }
  })
}

defineExpose({
  show,
})
</script>
<style lang="scss" scoped>
</style>

